<?php
namespace control;
/*
*单据追踪类
*/

class orderTrackClass extends adIndexClass
{
    private const title = '单据追踪';
    private const icon = root . 'img/control/order_icon.svg';

    private const bomBackGroundColor=['自制'=>'#adc6ff','委外'=>'#ffe1e0','虚拟'=>'#ffdeba','采购'=>'#9ed0ff'];

    public function bomPreview(): string
    {
        $id = $this->get('id');

        $data = [
            'id' => $id,
            'onion'=>[
                '供应链',
                '物料清单',
                'BOM组成'
            ]
        ];
        $tipHtml = '';
        foreach (self::bomBackGroundColor as $name=>$color){
            $tipHtml.="<span><div class='dot' style='background-color: {$color}'></div>&nbsp;{$name}</span>&emsp;&emsp;";
        }
        $data['html'] = <<<eof
<script src="{$this->root}static/javascript/x6.js"></script>
<script src="{$this->root}static/javascript/layout.min.js"></script>
<div>
<div class="operator-button"><span id="exportPng" class="spanButton li-btn-blue-bg">导出</span></div>
<div class="bom-title"><span style="margin-left: 100px"></span>{$tipHtml}</div>
<div class="canvas" id="canvas">
    
</div>
</div>
<input name="id" type="hidden" value="{$id}">
<script>
const { DagreLayout } = window.layout;
 X6.Graph.registerNode(
            'org-node', {
                width: 180,
                height: 100,
                markup: [{
                        tagName: 'rect',
                        selector: 'body',
                    },
                    {
                        tagName: 'text',
                        selector: 'materialCode',
                    },
                    {
                        tagName: 'text',
                        selector: 'materialName',
                    },
                     {
                        tagName: 'text',
                        selector: 'materialModel',
                    },
                    {
                        tagName: 'text',
                        selector: 'materialNum',
                    },
                ],
                attrs: {
                    data:{},
                    body: {
                        refWidth: '100%',
                        refHeight: '100%',
                        fill: '#FFFFFF',
                        stroke: '#e6e6e6',
                        strokeWidth: 1,
                        rx: 10,
                        ry: 10,
                    },
                    materialCode: {
                        refX: '50%',
                        refY: '20',
                        fontSize: 14,
                        textAnchor: 'middle'
                    },
                    materialName: {
                        refX: '50%',
                        refY: '38',
                        fontSize: 14,
                        textAnchor: 'middle'
                    },
                    materialModel: {
                        refX: '50%',
                        refY: '58',
                        fontSize: 14,
                        textAnchor: 'middle'
                    },
                    materialNum: {
                        refX: '50%',
                        refY: 80,
                        fontSize: 14,
                        textAnchor: 'middle',
                        //textDecoration: 'underline',
                    },
                },
            },
            true,
        )
        const graph = new X6.Graph({
            height: 790,
            container: document.getElementById('canvas'),
            background:{
              color: '#f5f5f7'  
            },
            // grid: true,
            //对齐线是移动节点排版的辅助工具
            snapline: true,
            connecting: {
                snap: true,
                highlight: true,
            },
            scroller: {
                enabled: true,
                pannable: true,
                pageVisible: true,
                pageBreak: false,
              },
            mousewheel: {
               enabled: true,
               modifiers: ['ctrl', 'meta'],
            },
        })
       
        const dagreLayout = new DagreLayout({
          type: 'dagre',
          rankdir: 'TB',
       begin:[500,100],
          ranksep: 30,
          nodesep: 10,
          controlPoints: true,
        })
        function member(x, y, item) {
            return graph.addNode({
                width: 180, // Number，可选，节点大小的 width 值
                height: 100, // Number，可选，节点大小的 height 值
                x:x,
                y:y,
                shape: 'org-node',
                attrs: {
                    body:{
                      fill:item.backgroundColor  
                    },
                     data:{
                            id:item.id,
                         },
                    materialCode: {
                       text: item.code,
                        fill: '#000',
                        fontSize: 12,
                    },
                    materialName: {
                        text: item.name,
                        fill: '#000',
                        fontSize: 12,
                    },
                     materialModel: {
                        text: item.model,
                        fill: '#000',
                        fontSize: 12,
                    },
                    materialNum: {
                        text: item.num,
                        fill: '#000',
                        fontSize: 12,
                    },
                }
            })
        }
        //生成边
        function reEdge(graph,from,to){
             let  edge = new X6.Shape.Edge({
                source: from,
                target: to,
                // router: {
                //     name: 'orth',
                //     args: {
                //           offset: 'center',
                //         },
                // },
                // attrs: {
                //     line: {
                //         stroke: '#555',
                //     },
                // },
            })
            graph.addEdge(edge)
        }
 
    //重新布局
    function reLayout() {
     let edges = []
      let nodes = []
      graph.toJSON().cells.forEach(cell => {
        if (cell.shape === "edge") {
          edges.push({
            source: cell.source.cell,
            target: cell.target.cell,
            router: {
                
                    args: {
                          offset: 'center',
                        },
                },
                attrs:{
                line:{
                     radius: 20,
      offset: 45,
      endArrow: true,
      lineWidth: 2,
      stroke: '#C2C8D5',
                }
                }
          })
        } else {
          nodes.push(cell)
        }
      });
     let newModel = dagreLayout.layout({
        nodes: nodes,
        edges: edges
      });
      graph.fromJSON(newModel);
   
       
    }
    $("#exportPng").on('click',function() {
            graph.toPNG((dataUri) => {
          // 下载
          X6.DataUri.downloadDataUri(dataUri, 'chart.png')
        })
    })
    const pos = 250;
    const height_pos = 160;
    function loopData(result,node=null,level=1) {
        result.forEach((item,index)=>{
            if (item){
                let nodeObj = member(level*pos, height_pos*(index+1), item);
             if (node){
                 reEdge(graph,node,nodeObj)
             }
             if (item.children.length >0){
                 loopData(item.children,nodeObj,level+1);
             }
            }
        })
    }
    function requestData(){
        let id = $("[name='id']").val();
        $.post(root+'control/adIndex.html?cla=orderTrack&fun=bomData',{id:id},function(res) {
            loopData(res.result);
            reLayout();
            // addEvent();
        },'json')
    }
    
    requestData();
     
</script>
<style>
.dot{display: inline-block;width: 20px;height: 20px;border-radius: 50%;vertical-align: text-bottom;}
.to-left{margin-left: 50px}
.canvas{height: 800px;width: 100vw;padding: 20px}
.operator-button{position: absolute;top: 0;left: 10px;width: 100px;height: 50px;text-align: center;line-height: 50px;z-index: 10}
.bom-title{width: 100%;height: 50px;line-height: 50px}
</style>
eof;
        return $this->tab($data);
    }

    public function home(): string
    {
        return $this->canvas($this->get('id'), $this->get('orderType'));
    }

    private function canvas($id, $orderType): string
    {
        $data = [
            'id' => $id,
            'onion'=>[
                '供应链',
                '单据追踪',
                '单据追踪'
            ]
        ];
        $data['html'] = <<<eof
<script src="{$this->root}static/javascript/x6.js"></script>
<script src="{$this->root}static/javascript/layout.min.js"></script>
<div class="canvas">
<div class="operator-button"><span id="exportPng" class="spanButton li-btn-blue-bg">导出</span></div>
    <input name="id" type="hidden" value="{$id}">
    <input name="orderType" type="hidden" value="{$orderType}">
<!--    <div class="left-canvas">-->
<!--        -->
<!--    </div>-->
    <div id="right-canvas" class="right-canvas">
    </div>
</div>
<script>
const { DagreLayout } = window.layout
 X6.Graph.registerNode(
            'org-node', {
                width: 180,
                height: 100,
                markup: [{
                        tagName: 'rect',
                        selector: 'body',
                    },
                    {
                        tagName: 'image',
                        selector: 'avatar',
                    },
                    {
                        tagName: 'text',
                        selector: 'orderNo',
                    },
                    {
                        tagName: 'text',
                        selector: 'orderType',
                    },
                ],
                attrs: {
                    data:{},
                    body: {
                        refWidth: '100%',
                        refHeight: '100%',
                        fill: '#FFFFFF',
                        stroke: '#e6e6e6',
                        strokeWidth: 1,
                        rx: 10,
                        ry: 10,
                    },
                    avatar: {
                        width: 40,
                        height: 40,
                        refX: 70,
                        refY: 6,
                    },
                    orderType: {
                       refX: '50%',
                        refY: '58',
                        fontSize: 14,
                        textAnchor: 'middle'
                    },
                    orderNo: {
                        refX: '50%',
                        refY: 80,
                        fontSize: 14,
                        textAnchor: 'middle',
                        
                        //textDecoration: 'underline',
                    },
                },
            },
            true,
        )
        const graph = new X6.Graph({
            height: 790,
            container: document.getElementById('right-canvas'),
            background:{
              color: '#f5f5f7'  
            },
            // grid: true,
            //对齐线是移动节点排版的辅助工具
            snapline: true,
            connecting: {
                snap: true,
                highlight: true,
            },
            scroller: {
                enabled: true,
                pannable: true,
                pageVisible: true,
                pageBreak: false,
              },
            mousewheel: {
               enabled: true,
               modifiers: ['ctrl', 'meta'],
            },
        })
        
        const dagreLayout = new DagreLayout({
          type: 'dagre',
          rankdir: 'LR',
          align: 'UL',
          ranksep: 30,
          nodesep: 10,
          controlPoints: true,
        })
         $("#exportPng").on('click',function() {
            graph.toPNG((dataUri) => {
          // 下载
          X6.DataUri.downloadDataUri(dataUri, 'chart.png')
        })
    })
        function member(x, y, item) {
            return graph.addNode({
                width: 180, // Number，可选，节点大小的 width 值
                height: 100, // Number，可选，节点大小的 height 值
                x:x,
                y:y,
                shape: 'org-node',
                attrs: {
                     data:{
                            id:item.id,
                            orderType:item.orderType,
                            orderNo:item.orderNo,
                            url:item.url,
                            power:item.power
                         },
                    avatar: {
                        opacity: 0.7,
                        'xlink:href': item.img,
                        
                    },
                    orderType: {
                        text: item.orderType,
                        fill: '#000',
                        fontSize: 12,
                    },
                    orderNo: {
                        text: item.orderNo,
                        fill: '#000',
                        fontSize: 12,
                    },
                }
            })
        }
        //生成边
        function reEdge(graph,from,to){
             let  edge = new X6.Shape.Edge({
                source: from,
                target: to,
                // router: {
                //     name: 'orth',
                //     args: {
                //           offset: 'center',
                //         },
                // },
                // attrs: {
                //     line: {
                //         stroke: '#555',
                //     },
                // },
            })
            graph.addEdge(edge)
        }
 
    //重新布局
    function reLayout() {
     let edges = []
      let nodes = []
      graph.toJSON().cells.forEach(cell => {
        if (cell.shape === "edge") {
          edges.push({
            source: cell.source.cell,
            target: cell.target.cell,
            router: {
                    name: 'manhattan',
                    args: {
                          offset: 'center',
                        },
                },
                attrs:{
                line:{
                     radius: 20,
      offset: 45,
      endArrow: true,
      lineWidth: 2,
      stroke: '#C2C8D5',
                }
                }
          })
        } else {
          nodes.push(cell)
        }
      });
     let newModel = dagreLayout.layout({
        nodes: nodes,
        edges: edges
      });
      graph.fromJSON(newModel)
    }
    
    const pos = 250;
    const height_pos = 160;
    function loopData(result,node=null,level=1) {
        result.forEach((item,index)=>{
            if (item){
                let nodeObj = member(level*pos, height_pos*(index+1), item);
             if (node){
                 reEdge(graph,node,nodeObj)
             }
             if (item.children.length >0){
                 loopData(item.children,nodeObj,level+1);
             }
            }
        })
    }
    function requestData(){
        let id = $("[name='id']").val();
        let orderType = $("[name='orderType']").val();
        $.post(root+'control/adIndex.html?cla=orderTrack&fun=orderData',{id:id,orderType:orderType},function(res) {
            loopData(res.result);
            reLayout();
            addEvent();
        },'json')
    }
    
    function addEvent(){
        graph.on('node:click', ({ e, x, y, node, view }) => {
            if (node.attrs.data.power){
                newIframe(node.attrs.data.orderType,node.attrs.data.url+'&id='+node.attrs.data.id)
            }else{
                msg('您还没有此页面的查看权限','error')
            }        
        })
    }
    requestData();
     
</script>
eof. $this->style();
        return $this->tab($data);
    }

    private function style(): string
    {
        return <<<eof
<style>
.canvas{display: flex;flex-direction: row;padding: 10px}
.left-canvas,.right-canvas{height: 800px;}
.left-canvas{width: 20%;margin:0 20px; border: 1px solid #e6e6e6;border-radius: 3px;overflow: auto}
.right-canvas{width: 100%;margin-left: 20px;padding: 5px; }
.v-line:hover{cursor: pointer}
.operator-button{position: absolute;top: 10px;left: 40px;width: 100px;height: 50px;text-align: center;line-height: 50px;z-index: 10}
</style>
eof;

    }

    public function bomData(){
        $bom = query('bom', " id = '{$this->post('id')}'");
        $material = query('material', "id = '$bom[materialId]'");
        $json['result'][]  = [
            'code' => $material['code'],
            'name' => $material['name'],
            'model'=>$material['model'],
            'num'=>self::lostZero($bom['num'],'numPrecision').$bom['unit'].'('.$material['source'].')',
            'id' => $bom['id'],
            'subject'=>$material['subject'],
            'backgroundColor'=>self::bomBackGroundColor[$material['source']],
            'children'=>self::bomLoop($bom['id'])
        ];
        return json_encode($json, 320);
    }

    public function bomLoop($id){
        $data = [];
        $result = find(['table' => 'bom_detail', 'where' => "orderId = '$id' order by list asc"]);
        // 角标
        if ($result['array']) {
            foreach ($result['array'] as $value) {
                $material = query('material',"id = '$value[materialId]'");
                if ($value['bomId']) {
                    $data[] = [
                        'code' => $material['code'],
                        'name' => $material['name'],
                        'model'=>$material['model'],
                        'num'=>self::lostZero($value['num'],'numPrecision').$value['unit'].'('.$material['source'].')',
                        'id' => $value['id'],
                        'subject'=>$material['subject'],
                        'backgroundColor'=>self::bomBackGroundColor[$material['source']],
                        'children'=>self::bomLoop($value['bomId'])
                    ];
                } else {

                    $data[] = [
                        'code' => $material['code'],
                        'name' => $material['name'],
                        'model'=>$material['model'],
                        'num'=>self::lostZero($value['num'],'numPrecision').$value['unit'].'('.$material['source'].')',
                        'id' => $value['id'],
                        'subject'=>$material['subject'],
                        'backgroundColor'=>self::bomBackGroundColor[$material['source']],
                        'children'=>[]
                    ];
                }
            }
        }
        return $data;
    }

    public function orderData(): bool|string
    {
        $orderType = $this->post('orderType');
        $id = $this->post('id');
        $json['result'] = $this->rootOrder($id, $orderType);
        return json_encode($json, 320);
    }



    /**
     * 根订单
     */
    public function rootOrder($id, $type): array
    {
        switch ($type) {
            case '采购申请单':
                $purchaseApp = query('purchase_apply', "id = '$id'");
                $result[] = [
                    'orderType' => '采购申请单',
                    'orderNo' => $purchaseApp['orderNo'],
                    'id' => $id,
                    'power'=>$this->power('purchaseApply','see'),
                    'img' => self::icon,
                    'sourceId' => '',
                    'sourceType' => '',
                    'url' => root . 'control/adIndex.html?cla=purchaseApply&fun=detail',
                    'children' => $this->purchaseOrder($id, $type)
                ];
                break;
            case '销售订单':
                $saleOrder = query('sale_order', "id = '$id'");
                $temp = [
                    'orderType' => '销售订单',
                    'orderNo' => $saleOrder['orderNo'],
                    'id' => $id,
                    'img' => self::icon,
                    'power'=>$this->power('saleOrder','see'),
                    'sourceId' => '',
                    'sourceType' => '',
                    'url' => root . 'control/adIndex.html?cla=saleOrder&fun=detail',
                ];
                //销售发货单
                $temp['children'] = $this->saleSendOrder($saleOrder['id']);
                //销售退货单
                $temp['children'] = array_merge($temp['children'], $this->saleRefundOrder($saleOrder['id'], $type));
                //委外加工单
                $temp['children'] = array_merge($temp['children'], $this->outsourceOrder($saleOrder['id'], $type));
                //采购订单
                $temp['children'] = array_merge($temp['children'], $this->purchaseOrder($saleOrder['id'], $type));
                //生产计划单
                $temp['children'] = array_merge($temp['children'], $this->machiningOrder($saleOrder['id'], $type));
                $result[] = $temp;
                break;
            case '采购订单':
                $result = $this->purchaseOrder($id,$type);
                $result = $this->purchaseOrderParent($id,$result);
                break;
            case '采购收货单':
                $result = $this->purchaseGetOrder($id,$type);
                $result = $this->purchaseGetOrderParent($id,$result);
                break;
            case '采购退货单':
                $result = $this->purchaseRefundOrder($id,$type);
                $result = $this->purchaseRefundOrderParent($id,$result);
                break;
            case '销售发货单':
                $result = $this->saleSendOrder($id,$type);
                $result = $this->saleSendOrderParent($id,$result);
                break;
            case '销售退货单':
                $result = $this->saleRefundOrder($id,$type);
                $result = $this->saleRefundOrderParent($id,$result);
                break;
            case '委外加工单':
                $result = $this->outsourceOrder($id,$type);
                $result = $this->outsourceOrderParent($id,$result);
                break;
            case '委外发料单':
                $result = $this->outsourcePickOrder($id,$type);
                $result = $this->outsourcePickOrderParent($id,$result);
                break;
            case '委外退料单':
                $result = $this->outsourceRefundPickOrder($id,$type);
                $result = $this->outsourceRefundPickOrderParent($id,$result);
                break;
            case '委外收货单':
                $result = $this->outsourceGetOrder($id,$type);
                $result = $this->outsourceGetOrderParent($id,$result);
                break;
            case '委外退货单':
                $result = $this->outsourceRefundOrder($id,$type);
                $result = $this->outsourceRefundOrderParent($id,$result);
                break;
            case '生产计划单':
                $result = $this->machiningOrder($id,$type);
                $result = $this->machiningOrderParent($id,$result);
                break;
            case '半成品加工单':
                $result = $this->semiOrder($id,$type);
                $result = $this->semiOrderParent($id,$result);
                break;
            case '生产入库单':
                $result = $this->machiningInOrder($id,$type);
                $result = $this->machiningInOrderParent($id,$result);
                break;
            case '生产派工单':
                $result = $this->machiningSendOrder($id,$type);
                $result = $this->machiningInOrderParent($id,$result,'生产派工单');
                break;
            case '生产流转卡':
                $result = $this->machiningCard($id,$type);
                $result = $this->machiningInOrderParent($id,$result,'生产流转卡');
                break;
            case '领料单':
                $result = $this->pickOrder($id,$type);
                $result = $this->machiningInOrderParent($id,$result,'领料单');
                break;
            case '退料单':
                $result = $this->refundPickOrder($id,$type);
                $result = $this->refundPickOrderParent($id,$result);
                break;
            case '委外派工单':
                $result = $this->outsourceSendOrder($id,$type);
                $result = $this->machiningInOrderParent($id,$result,'委外派工单');
                break;
            case '委外报工':
                $result = $this->outsourceReport($id,$type);
                $result = $this->outsourceReportParent($id,$result);
                break;
            case '配料单':
                $result = $this->ingredientsOrder($id,$type);
                $result = $this->machiningInOrderParent($id,$result,'配料单');
                break;
            case '销售预测单':
                $result = $this->saleForecast($id,$type);
                break;
            default:
                $result[] = [];
                break;
        }
        return $result;
    }

    /**
     * Desc : 采购订单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function purchaseOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("purchase_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceTable = 'purchase_apply_detail' and sourceId <> '' ","sourceId");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            $sourceOrderDetail = query("purchase_apply_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
            $array             = query("purchase_apply", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
            $result = [
                [
                    'orderType' => '采购申请单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'power'=>$this->power('purchaseApply','see'),
                    'img' => self::icon,
                    'sourceId' => '',
                    'sourceType' => '',
                    'url' => root . 'control/adIndex.html?cla=purchaseApply&fun=detail',
                    'children' => $result
                ]
            ];
        }
        return $result;
    }
    /**
     * Desc : 采购收货单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function purchaseGetOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("purchase_get_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            $sourceOrderDetail = query("purchase_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId,sourceTable,sourceId");
            $array             = query("purchase_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
            $result = [
                [
                    'power'=>$this->power('purchaseOrder','see'),
                    'orderType' => '采购订单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceOrderDetail['sourceTable']=='purchase_apply_detail'?$sourceOrderDetail['sourceId']:'',
                    'sourceType' => $sourceOrderDetail['sourceTable']=='purchase_apply_detail'?'采购申请单':'',
                    'url' => root . 'control/adIndex.html?cla=purchaseOrder&fun=detail',
                    'children' => $result
                ]
            ];
            $result = $this->purchaseOrderParent($array['id'],$result);
        }
        return $result;
    }
    /**
     * Desc : 采购退货单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function purchaseRefundOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("purchase_refund_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId,sourceTable");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            if($orderDetail['sourceTable'] == 'purchase_order_detail'){
                $sourceOrderDetail = query("purchase_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId,sourceTable,sourceId");
                $array             = query("purchase_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'power'=>$this->power('purchaseOrder','see'),
                        'orderType' => '采购订单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => $sourceOrderDetail['sourceTable']=='purchase_apply_detail'?$sourceOrderDetail['sourceId']:'',
                        'sourceType' => $sourceOrderDetail['sourceTable']=='purchase_apply_detail'?'采购申请单':'',
                        'url' => root . 'control/adIndex.html?cla=purchaseOrder&fun=detail',
                        'children' => $result
                    ]
                ];
                $result = $this->purchaseOrderParent($array['id'],$result);
            }elseif($orderDetail['sourceTable'] == 'purchase_get_order_detail'){
                $sourceOrderDetail = query("purchase_get_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId,sourceId");
                $array             = query("purchase_get_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'power'=>$this->power('purchaseGetOrder','see'),
                        'orderType' => '采购收货单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => $sourceOrderDetail['sourceId']?$sourceOrderDetail['sourceId']:'',
                        'sourceType' => $sourceOrderDetail['sourceId']?'采购订单':'',
                        'url' => root . 'control/adIndex.html?cla=purchaseGetOrder&fun=detail',
                        'children' => $result
                    ]
                ];
                $result = $this->purchaseGetOrderParent($array['id'],$result);
            }
        }
        return $result;
    }
    /**
     * Desc : 销售发货单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function saleSendOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("sale_send_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and saleOrderMxId <> '' ","saleOrderMxId");
        if(isset($orderDetail['saleOrderMxId']) && $orderDetail['saleOrderMxId']) {
            $sourceOrderDetail = query("sale_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['saleOrderMxId']."' ","orderId");
            $array             = query("sale_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
            $result = [
                [
                    'orderType' => '销售订单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('saleOrder','see'),
                    'sourceId' => '',
                    'sourceType' => '',
                    'url' => root . 'control/adIndex.html?cla=saleOrder&fun=detail',
                    'children' => $result
                ]
            ];
        }
        return $result;
    }
    /**
     * Desc : 销售退货单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function saleRefundOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("sale_refund_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId,sourceTable");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            if($orderDetail['sourceTable'] == 'sale_order_detail'){
                $sourceOrderDetail = query("sale_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                $array             = query("sale_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'orderType' => '销售订单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'power'=>$this->power('saleOrder','see'),
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=saleOrder&fun=detail',
                        'children' => $result
                    ]
                ];
            }elseif($orderDetail['sourceTable'] == 'sale_send_order_detail'){
                $sourceOrderDetail = query("sale_send_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId,saleOrderMxId");
                $array             = query("sale_send_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'power'=>$this->power('saleSendOrder','see'),
                        'orderType' => '销售发货单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => $sourceOrderDetail['saleOrderMxId']?$sourceOrderDetail['saleOrderMxId']:'',
                        'sourceType' => $sourceOrderDetail['saleOrderMxId']?'销售订单':'',
                        'url' => root . 'control/adIndex.html?cla=saleSendOrder&fun=detail',
                        'children' => $result
                    ]
                ];
                $result = $this->saleSendOrderParent($array['id'],$result);
            }
        }
        return $result;
    }
    /**
     * Desc : 委外加工单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function outsourceOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("outsource_process_material"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId,sourceTable");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            $outsource_demands = query("outsource_demands"," id = '".$orderDetail['sourceId']."' ","sourceId,sourceTable");
            if($outsource_demands['sourceTable'] == 'sale_order'){
                $array             = query("sale_order", " coid = '" . self::$coid . "' and id = '".$outsource_demands['sourceId']."' ");
                $result = [
                    [
                        'orderType' => '销售订单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'power'=>$this->power('saleOrder','see'),
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=saleOrder&fun=detail',
                        'children' => $result
                    ]
                ];
            }elseif($outsource_demands['sourceTable'] == 'machining_order'){
                $array             = query("machining_order", " coid = '" . self::$coid . "' and id = '".$outsource_demands['sourceId']."' ");
                $result = [
                    [
                        'power'=>$this->power('machiningOrder','see'),
                        'orderType' => '生产计划单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=machiningOrder&fun=detail',
                        'children' => $result
                    ]
                ];
                $result = $this->machiningOrderParent($array['id'],$result);
            }
        }
        return $result;
    }
    /**
     * Desc : 委外发料单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function outsourcePickOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("outsource_pick_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceTable = 'outsource_process_detail' and sourceId <> '' ","sourceId");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            $sourceOrderDetail = query("outsource_process_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
            $array             = query("outsource_process", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
            $result = [
                [
                    'power'=>$this->power('outsourceProcess','see'),
                    'orderType' => '委外加工单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => '',
                    'sourceType' => '',
                    'url' => root . 'control/adIndex.html?cla=outsourceProcess&fun=detail',
                    'children' => $result
                ]
            ];
            $result = $this->outsourceOrderParent($array['id'],$result);
        }
        return $result;
    }
    /**
     * Desc : 委外退料单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function outsourceRefundPickOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("outsource_pick_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId,sourceTable");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            if($orderDetail['sourceTable'] == 'outsource_process_detail'){
                $sourceOrderDetail = query("outsource_process_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                $array             = query("outsource_process", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'power'=>$this->power('outsourceProcess','see'),
                        'orderType' => '委外加工单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=outsourceProcess&fun=detail',
                        'children' => $result
                    ]
                ];
                $result = $this->outsourceOrderParent($array['id'],$result);
            }elseif($orderDetail['sourceTable'] == 'outsource_pick_order_detail'){
                $sourceOrderDetail = query("outsource_pick_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                $array             = query("outsource_pick_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'orderType' => '委外发料单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'power'=>$this->power('outsourcePick','see'),
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=outsourcePick&fun=detail',
                        'children' => $result
                    ]
                ];
                $result = $this->outsourcePickOrderParent($array['id'],$result);
            }
        }
        return $result;
    }
    /**
     * Desc :委外收货单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function outsourceGetOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("outsource_in_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            $sourceOrderDetail = query("outsource_process_material", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","sourceId,orderId");
            $array             = query("outsource_process", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
            $result = [
                [
                    'power'=>$this->power('outsourceProcess','see'),
                    'orderType' => '委外加工单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => '',
                    'sourceType' => '',
                    'url' => root . 'control/adIndex.html?cla=outsourceProcess&fun=detail',
                    'children' => $result
                ]
            ];
            $result = $this->outsourceOrderParent($array['id'],$result);
        }
        return $result;
    }
    /**
     * Desc :委外退货单单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function outsourceRefundOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("outsource_refund_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            $sourceOrderDetail = query("outsource_in_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","sourceId,orderId");
            $array             = query("outsource_in_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
            $result = [
                [
                    'orderType' => '委外收货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('outsourceGet','see'),
                    'sourceId' => $sourceOrderDetail['sourceId']?$sourceOrderDetail['sourceId']:'',
                    'sourceType' => $sourceOrderDetail['sourceId']?'委外加工单':'',
                    'url' => root . 'control/adIndex.html?cla=outsourceGet&fun=detail',
                    'children' => $result
                ],
            ];
            $result = $this->outsourceGetOrderParent($array['id'],$result);
        }
        return $result;
    }
    /**
     * Desc : 生产计划单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function machiningOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("machining_order_mx"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId,sourceTable");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            if($orderDetail['sourceTable'] == 'sale_order_detail'){
                $sourceOrderDetail = query("sale_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                $array             = query("sale_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'orderType' => '销售订单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'power'=>$this->power('saleOrder','see'),
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=saleOrder&fun=detail',
                        'children' => $result
                    ]
                ];
            }elseif($orderDetail['sourceTable'] == 'sale_forecast_detail'){
                $sourceOrderDetail = query("sale_forecast_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                $array             = query("sale_forecast", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'orderType' => '销售预测单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => '',
                        'sourceType' => '',
                        'power'=>$this->power('saleForecast','see'),
                        'url' => root . 'control/adIndex.html?cla=saleForecast&fun=detail',
                        'children' => $result
                    ]
                ];
            }
        }
        return $result;
    }
    /**
     * Desc : 半成品加工单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function semiOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("machining_order_mx"," coid = '" . self::$coid . "' and orderId = '$id' and sourceTable = 'semi_demands' and sourceId <> '' ","sourceId");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']) {
            $sourceOrderDetail = query("semi_demands", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","sourceId");
            if($sourceOrderDetail['sourceId']) {
                $array  = query("machining_order", " coid = '" . self::$coid . "' and id = '" . $sourceOrderDetail['sourceId'] . "' ");
                $result = [
                    [
                        'power'=>$this->power('machiningOrder','see'),
                        'orderType' => '生产计划单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=machiningOrder&fun=detail',
                        'children'   => $result
                    ]
                ];
                $result = $this->machiningOrderParent($array['id'],$result);
            }
        }
        return $result;
    }
    /**
     * Desc : 生产入库单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @param string $type
     * @return array
     */
    public function machiningInOrderParent($id, $children = [], $type = '生产入库单'): array
    {
        $result = $children;
        switch ($type) {
            case '生产入库单':
                $orderDetail = query("machining_in_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","sourceId");
                $sourceOrderDetail = query("machining_order_mx", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                break;
            case '生产派工单':
            case '委外派工单':
                $orderDetail = query("machining_send_order"," coid = '" . self::$coid . "' and id = '$id' ","machiningOrderMxId");
                $sourceOrderDetail = query("machining_order_mx", " coid = '" . self::$coid . "' and id = '".$orderDetail['machiningOrderMxId']."' ","orderId");
                break;
            case '生产流转卡':
                $orderDetail = query("machining_card"," coid = '" . self::$coid . "' and id = '$id' ","sourceMxId");
                $sourceOrderDetail = query("machining_order_mx", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceMxId']."' ","orderId");
                break;
            case '领料单':
            case '退料单':
                $orderDetail = query("pick_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceTable = 'machining_order_detail' and sourceId <> '' ","sourceId");
                $sourceOrderDetail = query("machining_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                break;
            case '配料单':
                $orderDetail = query("ingredients_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceTable = 'ingredients_demands' and sourceId <> '' ","sourceId");
                $sourceOrderDetail = query("ingredients_demands", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","machiningOrderId");
                $sourceOrderDetail['orderId'] = $sourceOrderDetail['machiningOrderId'];
                break;
        }
        if(isset($sourceOrderDetail['orderId']) && $sourceOrderDetail['orderId']) {
            $array  = query("machining_order", " coid = '" . self::$coid . "' and id = '" . $sourceOrderDetail['orderId'] . "' ");
            if($array['type'] == '生产计划'){
                $result = [
                    [
                        'power'=>$this->power('machiningOrder','see'),
                        'orderType' => '生产计划单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=machiningOrder&fun=detail',
                        'children'   => $result
                    ]
                ];
                $result = $this->machiningOrderParent($array['id'],$result);
            }else{
                $result = [
                    [
                        'power'=>$this->power('semiMachiningOrder','see'),
                        'orderType' => '半成品加工单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=semiMachiningOrder&fun=detail',
                        'children'   => $result
                    ]
                ];
                $result = $this->semiOrderParent($array['id'],$result);
            }
        }
        return $result;
    }
    /**
     * Desc : 退料单父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function refundPickOrderParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("pick_order_detail"," coid = '" . self::$coid . "' and orderId = '$id' and sourceId <> '' ","orderId,sourceTable,sourceId");
        if(isset($orderDetail['sourceId']) && $orderDetail['sourceId']){
            if($orderDetail['sourceTable'] == 'machining_order_detail'){
                $result = $this->machiningInOrderParent($orderDetail['orderId'],$result); //生产计划单/半成品加工单
            }elseif($orderDetail['sourceTable'] == 'pick_order_detail'){  //领料单
                $sourceOrderDetail = query("pick_order_detail", " coid = '" . self::$coid . "' and id = '".$orderDetail['sourceId']."' ","orderId");
                $array             = query("pick_order", " coid = '" . self::$coid . "' and id = '".$sourceOrderDetail['orderId']."' ");
                $result = [
                    [
                        'power'=>$this->power('pickOrder','see'),
                        'orderType' => '领料单',
                        'orderNo' => $array['orderNo'],
                        'id' => $array['id'],
                        'img' => self::icon,
                        'sourceId' => '',
                        'sourceType' => '',
                        'url' => root . 'control/adIndex.html?cla=pickOrder&fun=detail',
                        'children'   => $result
                    ]
                ];
                $result = $this->machiningInOrderParent($array['id'],$result,'领料单');
            }
        }
        return $result;
    }
    /**
     * Desc :委外报工父级
     * User : you name
     * Date : 2023-11-23 11:33
     * @param $id
     * @param array $children
     * @return array
     */
    public function outsourceReportParent($id, $children = []): array
    {
        $result = $children;
        $orderDetail = query("process_report"," coid = '" . self::$coid . "' and id = '$id' and sendId <> '' ","sendId");
        if(isset($orderDetail['sendId']) && $orderDetail['sendId']) {
            $array = query("machining_send_order"," coid = '" . self::$coid . "' and id = '".$orderDetail['sendId']."' ");
            $result = [
                [
                    'orderType' => '委外派工单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('outsourceSendOrder','see'),
                    'sourceId' => '',
                    'sourceType' => '',
                    'url' => root . 'control/adIndex.html?cla=outsourceSendOrder&fun=detail',
                    'children' => $result
                ],
            ];
            $result = $this->machiningInOrderParent($array['id'],$result,'委外派工单');
        }
        return $result;
    }
    /**
     * Desc : 查找父级订单
     * User : you name
     * Date : 2023-11-23 11:21
     * @param $id
     * @param $type
     * @param array $children
     * @return array
     */
    public function parentOrder($id, $type, $children = []): array
    {
        switch ($type) {
            case '配料单':
                $result = $this->ingredientsOrder($id,$type);
                break;
            default:
                $result[] = [];
                break;
        }
        return $result;
    }
    /**
     * 销售预测单
     * 从销售预测单生成（传销售预测单ID）
     */
    public function saleForecast($sourceId, $sourceType = '销售预测单'): array
    {
        $saleOrder = query('sale_forecast', "id = '$sourceId'");
        $temp = [
            'orderType' => '销售预测单',
            'orderNo' => $saleOrder['orderNo'],
            'id' => $sourceId,
            'img' => self::icon,
            'sourceId' => '',
            'sourceType' => '',
            'power'=>$this->power('saleForecast','see'),
            'url' => root . 'control/adIndex.html?cla=saleForecast&fun=detail',
        ];
        //销售发货单
        $temp['children'] = $this->machiningOrder($saleOrder['id'],'销售预测单');
        $result[] = $temp;
        return $result;
    }
    /**
     * 配料单
     * 从生产计划单生成（传生产计划单ID）
     * 从半成品加工单生成（传半成品加工单ID）
     * 从配料单生成（传入配料单ID）
     */
    public function ingredientsOrder($sourceId, $sourceType = '生产计划单'): array
    {
        $result = [];
        if ($sourceType == '生产计划单' || $sourceType == '半成品加工单') {
            $all = find(array('table' => 'ingredients_order', 'where' => "coid = '" . self::$coid . "' and  id in (select distinct orderId from ingredients_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from ingredients_demands where machiningOrderId = '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '配料单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'power'=>$this->power('ingredientsOrder','see'),
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=ingredientsOrder&fun=detail',
                    'children' => $this->outStock($array['id'],'配料单')
                ];
                $result[] = $temp;
            }
        }elseif($sourceType =='配料单'){
            $info = query('ingredients_order', "id = '$sourceId'");
            $temp = [
                'orderType' => '配料单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'power'=>$this->power('ingredientsOrder','see'),
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=ingredientsOrder&fun=detail',
                'children' => $this->outStock($info['id'],'配料单')
            ];
            $result[] = $temp;
        }
        return $result;
    }
    /**
     * 报废申请单
     * 从生产派工单生成（传入生产派工单ID）
     */
    public function scrappedOrder($sourceId, $sourceType = '生产派工单'): array
    {
        $result = [];
        if ($sourceType == '生产派工单') {
            $all = find(array('table' => 'scrapped_order', 'where' => "coid = '" . self::$coid . "' and sendId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '报废申请单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'power'=>$this->power('scrappedOrder','see'),
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=scrappedOrder&fun=detail',
                ];
                $temp['children'] = $this->inStock($array['id'], '报废申请单');
                $result[] = $temp;
            }
        }
        return $result;
    }

    /**
     * 委外发料单
     * 从生产计划单生成（传生产计划单ID）
     * 从半成品加工单生成（传半成品加工单ID）
     */
    public function outsourcePickOrder($sourceId, $sourceType = '委外加工单'): array
    {
        $result = [];
        if ($sourceType == '委外加工单') {
            $all = find(array('table' => 'outsource_pick_order', 'where' => "coid = '" . self::$coid . "' and type=0 and id in (select distinct orderId from outsource_pick_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_process_detail where orderId ='$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '委外发料单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('outsourcePick','see'),
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourcePick&fun=detail',
                ];
                $temp['children'] = $this->outsourceRefundPickOrder($array['id'], '委外发料单');
                $temp['children'] = array_merge($temp['children'], $this->outStock($array['id'], '委外发料单'));
                $result[] = $temp;
            }
        }elseif($sourceType =='委外发料单'){
            $info = query('outsource_pick_order', "id = '$sourceId'");
            $temp = [
                'orderType' => '委外发料单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'power'=>$this->power('outsourcePick','see'),
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=outsourcePick&fun=detail',
            ];
            $temp['children'] = $this->outsourceRefundPickOrder($info['id'], '委外发料单');
            $temp['children'] = array_merge($temp['children'], $this->outStock($info['id'], '委外发料单'));
            $result[] = $temp;
        }
        return $result;
    }
    /**
     * 委外退料单
     * 从生产计划单生成（传生产计划单ID）
     * 从半成品加工单生成（传半成品加工单ID）
     * 从领料单生成（传入领料单ID）
     */
    public function outsourceRefundPickOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '委外加工单') {
            $all = find(array('table' => 'outsource_pick_order', 'where' => "coid = '" . self::$coid . "' and type=1 and id in (select distinct orderId from outsource_pick_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_process_detail where orderId ='$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '委外退料单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('outsourceProcess','see'),
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourcePickRefund&fun=detail',
                    'children' => $this->inStock($array['id'], '委外退料单')
                ];
                $result[] = $temp;
            }
        } elseif ($sourceType == '委外发料单') {
            $all = find(array('table' => 'outsource_pick_order', 'where' => "coid = '" . self::$coid . "' and type=1 and id in (select distinct orderId from outsource_pick_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_pick_order_detail where orderId ='$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '委外退料单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'power'=>$this->power('outsourceProcess','see'),
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourcePickRefund&fun=detail',
                    'children' => $this->inStock($array['id'], '委外退料单')
                ];
                $result[] = $temp;
            }
        }elseif($sourceType =='委外退料单'){
            $info = query('outsource_pick_order', "id = '$sourceId'");
            $temp = [
                'orderType' => '委外退料单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'power'=>$this->power('outsourceProcess','see'),
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=outsourcePickRefund&fun=detail',
                'children' => $this->inStock($info['id'], '委外退料单')
            ];
            $result[] = $temp;
        }
        return $result;
    }


    /**
     * 委外收货单
     * 从委外加工单生成（传入委外加工单ID）
     * @param $sourceId
     * @param string $sourceType
     * @return array
     */
    public function outsourceGetOrder($sourceId, $sourceType = '委外加工单'): array
    {
        $result = [];
        if ($sourceType =='委外加工单'){
            $all = find(array('table' => 'outsource_in_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from outsource_in_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_process_material where orderId = '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '委外收货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('outsourceGet','see'),
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourceGet&fun=detail',
                ];
                $temp['children'] = $this->inStock($array['id'], '委外收货单');
                $temp['children'] = array_merge($temp['children'], $this->qualityOrder($array['id'], '委外收货单'));
                $temp['children'] = array_merge($temp['children'], $this->outsourceRefundOrder($array['id']));
                $result[] = $temp;
            }
        }elseif($sourceType =='委外收货单'){
            $info = query('outsource_in_order', "id = '$sourceId'");
            $temp = [
                'orderType' => '委外收货单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'power'=>$this->power('outsourceGet','see'),
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=outsourceGet&fun=detail',
            ];
            $temp['children'] = $this->inStock($info['id'], '委外收货单');
            $temp['children'] = array_merge($temp['children'], $this->qualityOrder($info['id'], '委外收货单'));
            $temp['children'] = array_merge($temp['children'], $this->outsourceRefundOrder($info['id']));
            $result[] = $temp;
        }

        return $result;
    }

    /**
     * 委外退货单
     * 从委外收货单生成（传入委外收货单ID）
     */
    public function outsourceRefundOrder($sourceId, $sourceType = '委外收货单'): array
    {
        $result = [];
        if ($sourceType =='委外收货单'){
            $all = find(array('table' => 'outsource_refund_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from outsource_refund_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_in_order_detail where orderId = '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '委外退货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('outsourceRefund','see'),
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourceRefund&fun=detail',
                ];
                $temp['children'] = $this->outStock($array['id'], '委外退货单');
                $result[] = $temp;
            }
        }elseif($sourceType =='委外加工单'){
            $info = query('outsource_refund_order', "id = '$sourceId'");
            $temp = [
                'orderType' => '委外退货单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'power'=>$this->power('outsourceRefund','see'),
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=outsourceRefund&fun=detail',
            ];
            $temp['children'] = $this->outStock($info['id'], '委外退货单');
            $result[] = $temp;
        }

        return $result;
    }


    /**
     * 委外派工单
     * 从生产加工单生成（传入生产加工单ID）
     * 从半成品加工单生成（传入半成品加工单ID）
     *
     */
    public function outsourceSendOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '生产计划单' || $sourceType == '半成品加工单') {
            $all = find(array('table' => 'machining_send_order', 'where' => "coid = '" . self::$coid . "' and classify = '委外派工' and machiningOrderId ='$sourceId' order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'orderType' => '委外派工单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'power'=>$this->power('outsourceSendOrder','see'),
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourceSendOrder&fun=detail',
                    'children' => $this->outsourceReport($array['id'])
                ];
                $result[] = $temp;
            }
        }elseif($sourceType =='委外派工单'){
            $info = query('machining_send_order', "id = '$sourceId'");
            $temp = [
                'orderType' => '委外派工单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'power'=>$this->power('outsourceSendOrder','see'),
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=outsourceSendOrder&fun=detail',
                'children' => $this->outsourceReport($info['id'])
            ];
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 委外报工单
     * 从委外派工单生成（传入委外派工单ID）
     *
     */
    public function outsourceReport($sourceId, $sourceType = '委外派工单'): array
    {
        $result = [];
        if ($sourceType = '委外派工单'){
            $all = find(array('table' => 'process_report', 'where' => "coid = '" . self::$coid . "' and classify = '委外报工'  and sendId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('outProcessReport','see'),
                    'orderType' => '委外报工',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outProcessReport&fun=detail',
                    'children' => $this->qualityOrder($array['id'], '委外报工')
                ];
                $result[] = $temp;
            }
        }elseif($sourceType =='委外报工'){
            $info = query('process_report', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('outProcessReport','see'),
                'orderType' => '委外报工',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=outProcessReport&fun=detail',
                'children' => $this->qualityOrder($info['id'], '委外报工')
            ];
            $result[] = $temp;
        }

        return $result;
    }

    /**
     * 委外加工单
     * 从委外需求来（销售单：传入销售单ID）
     * 从委外需求来（生产计划单：传入生产计划单ID）
     */
    public function outsourceOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '销售订单' || $sourceType =='生产计划单') {
            $all = find(array('table' => 'outsource_process', 'where' => "coid = '" . self::$coid . "'  and id in (select distinct orderId from outsource_process_material where coid = '" . self::$coid . "' and sourceId in (select id from outsource_demands where sourceId ='$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('outsourceProcess','see'),
                    'orderType' => '委外加工单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourceProcess&fun=detail',
                ];
                $temp['children'] = $this->outsourcePickOrder($array['id']);
                $temp['children'] = array_merge($temp['children'], $this->outsourceGetOrder($array['id']));
                $temp['children'] = array_merge($temp['children'], $this->outsourceRefundPickOrder($array['id'], '委外加工单'));
                $result[] = $temp;
            }
        }elseif($sourceType =='委外加工单'){
            $info = query('outsource_process', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('outsourceProcess','see'),
                'orderType' => '委外加工单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=outsourceProcess&fun=detail',
            ];
            $temp['children'] = $this->outsourcePickOrder($info['id']);
            $temp['children'] = array_merge($temp['children'], $this->outsourceGetOrder($info['id']));
            $temp['children'] = array_merge($temp['children'], $this->outsourceRefundPickOrder($info['id'], '委外加工单'));
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 质检单
     * 从工序报工来（传入工序报工ID）
     * 从采购收货单来（传入采购收货单ID）
     * 从生产入库单来（传入生产入库单ID）
     * 从销售发货单来（传入销售发货单ID）
     * 从委外收货单来（传入委外收货单ID）
     */
    public function qualityOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '工序报工' || $sourceType == '委外报工') {
            $all = find(array('table' => 'quality_order', 'where' => "coid = '" . self::$coid . "' and type=4 and sourceId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('processingInspection','see'),
                    'orderType' => '制程检验',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=processingInspection&fun=detail',
                    'children' => []
                ];
                $result[] = $temp;
            }
        } elseif ($sourceType == '采购收货单') {
            $all = find(array('table' => 'quality_order', 'where' => "coid = '" . self::$coid . "' and type=1 and sourceId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('materialInspection','see'),
                    'orderType' => '来料检验',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=materialInspection&fun=detail',
                    'children' => []
                ];
                $result[] = $temp;
            }
        } elseif ($sourceType == '生产入库单') {
            $all = find(array('table' => 'quality_order', 'where' => "coid = '" . self::$coid . "' and type=2 and sourceId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('processInspection','see'),
                    'orderType' => '制终检验',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=processInspection&fun=detail',
                    'children' => []
                ];
                $result[] = $temp;
            }
        } elseif ($sourceType == '销售发货单') {
            $all = find(array('table' => 'quality_order', 'where' => "coid = '" . self::$coid . "' and type=3 and sourceId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('shippingInspection','see'),
                    'orderType' => '发货检验',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=shippingInspection&fun=detail',
                    'children' => []
                ];
                $result[] = $temp;
            }
        } elseif ($sourceType == '委外收货单') {
            $all = find(array('table' => 'quality_order', 'where' => "coid = '" . self::$coid . "' and type=5 and sourceId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('outsourceInspection','see'),
                    'orderType' => '委外入库检验',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outsourceInspection&fun=detail',
                    'children' => []
                ];
                $result[] = $temp;
            }
        }
        return $result;
    }


    /**
     * 工序报工
     * 从派工单中生成（传入派工单ID）
     */
    public function processReport($sourceId, $sourceType = '生产派工单'): array
    {
        $result = [];
        if ($sourceType == '生产派工单') {
            $all = find(array('table' => 'process_report', 'where' => "coid = '" . self::$coid . "' and classify = '工序报工' and sendId ='$sourceId' order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('processReport','see'),
                    'orderType' => '工序报工',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=processReport&fun=detail',
                    'children' => $this->qualityOrder($array['id'], '工序报工')
                ];
                $result[] = $temp;
            }
        }elseif($sourceType =='生产流转卡'){
            $all = find(array('table' => 'process_report', 'where' => "coid = '" . self::$coid . "' and classify = '工序报工' and sendId in (select id from machining_card_detail where orderId = '$sourceId') order by orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('processReport','see'),
                    'orderType' => '工序报工',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=processReport&fun=detail',
                    'children' => $this->qualityOrder($array['id'], '工序报工')
                ];
                $result[] = $temp;
            }
        }

        return $result;
    }

    /**
     * 生产流转卡
     * 从生产计划单生成（传入生产计划单ID）
     * 从半成品加工单生成（传半成品加工单ID）
     */
    public function machiningCard($sourceId, $sourceType){
        $result = [];
        if ($sourceType == '生产计划单' || $sourceType == '半成品加工单') {
            $all = find(array('table' => 'machining_card', 'where' => "coid = '" . self::$coid . "' and sourceId ='$sourceId' order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('machiningCard','see'),
                    'orderType' => '生产流转卡',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=machiningCard&fun=detail',
                ];
                $temp['children'] = $this->processReport($array['id'],'生产流转卡');
//                $temp['children'] = array_merge($temp['children'], $this->scrappedOrder($array['id']));
                $result[] = $temp;
            }
        }elseif($sourceType =='生产流转卡'){
            $info = query('machining_card', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('machiningCard','see'),
                'orderType' => '生产流转卡',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=machiningCard&fun=detail',
            ];
            $temp['children'] = $this->processReport($info['id'],'生产流转卡');
//            $temp['children'] = array_merge($temp['children'], $this->scrappedOrder($info['id']));
            $result[] = $temp;
        }
        return $result;
    }
    /**
     * 生产派工单
     * 从生产计划单生成（传入生产计划单ID）
     * 从半成品加工单生成（传半成品加工单ID）
     */
    public function machiningSendOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '生产计划单' || $sourceType == '半成品加工单') {
            $all = find(array('table' => 'machining_send_order', 'where' => "coid = '" . self::$coid . "' and classify = '生产派工' and machiningOrderId ='$sourceId' order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('machiningSendOrder','see'),
                    'orderType' => '生产派工单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=machiningSendOrder&fun=detail',
                ];
                $temp['children'] = $this->processReport($array['id']);
                $temp['children'] = array_merge($temp['children'], $this->scrappedOrder($array['id']));
                $result[] = $temp;
            }
        }elseif($sourceType =='生产派工单'){
            $info = query('machining_send_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('machiningSendOrder','see'),
                'orderType' => '生产派工单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=machiningSendOrder&fun=detail',
            ];
            $temp['children'] = $this->processReport($info['id']);
            $temp['children'] = array_merge($temp['children'], $this->scrappedOrder($info['id']));
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 生产领料单
     * 从生产计划单生成（传生产计划单ID）
     * 从半成品加工单生成（传半成品加工单ID）
     */
    public function pickOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '生产计划单' || $sourceType == '半成品加工单') {
            $all = find(array('table' => 'pick_order', 'where' => "coid = '" . self::$coid . "' and type=0 and id in (select distinct orderId from pick_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from machining_order_detail where orderId ='$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('pickOrder','see'),
                    'orderType' => '领料单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=pickOrder&fun=detail',

                ];
                $temp['children'] = $this->refundPickOrder($array['id'], '领料单');
                $temp['children'] = array_merge($temp['children'],$this->outStock($array['id'], '领料单'));
                $result[] = $temp;
            }
        }elseif($sourceType =='领料单'){
            $info = query('pick_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('pickOrder','see'),
                'orderType' => '领料单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=pickOrder&fun=detail',
            ];
            $temp['children'] = $this->refundPickOrder($info['id'], '领料单');
            $temp['children'] = array_merge($temp['children'],$this->outStock($info['id'], '领料单'));
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 生产退料单
     * 从生产计划单生成（传生产计划单ID）
     * 从半成品加工单生成（传半成品加工单ID）
     * 从领料单生成（传入领料单ID）
     */
    public function refundPickOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '生产计划单' || $sourceType == '半成品加工单') {
            $all = find(array('table' => 'pick_order', 'where' => "coid = '" . self::$coid . "' and type=1 and id in (select distinct orderId from pick_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from machining_order_detail where orderId ='$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('refundMaterialOrder','see'),
                    'orderType' => '退料单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=refundMaterialOrder&fun=detail',
                    'children' => $this->inStock($array['id'], '退料单')
                ];
                $result[] = $temp;
            }
        } elseif ($sourceType == '领料单') {
            $all = find(array('table' => 'pick_order', 'where' => "coid = '" . self::$coid . "' and type=1 and id in (select distinct orderId from pick_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from pick_order_detail where orderId ='$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('refundMaterialOrder','see'),
                    'orderType' => '退料单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=refundMaterialOrder&fun=detail',
                    'children' => $this->inStock($array['id'], '退料单')
                ];
                $result[] = $temp;
            }
        }elseif($sourceType =='退料单'){
            $info = query('pick_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('refundMaterialOrder','see'),
                'orderType' => '退料单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=refundMaterialOrder&fun=detail',
                'children' => $this->inStock($info['id'], '退料单')
            ];
            $result[] = $temp;
        }
        return $result;
    }
    /**
     * 生产计划单
     * 从产品需求生成（传入销售订单ID）
     */
    public function machiningOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '销售订单') {
            $all = find(array('table' => 'machining_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from machining_order_mx where coid = '" . self::$coid . "' and (sourceId in (select id from product_demands where sourceId =  '$sourceId') or sourceId in (select id from sale_order_detail where orderId =  '$sourceId'))) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('machiningOrder','see'),
                    'orderType' => '生产计划单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=machiningOrder&fun=detail',
                ];
                $temp['children'] = $this->machiningInOrder($array['id'], '生产计划单');
                $temp['children'] = array_merge($temp['children'], $this->machiningSendOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->machiningCard($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->semiOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->outsourceSendOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->pickOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->refundPickOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->outsourceOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->ingredientsOrder($array['id']));
                $result[] = $temp;
            }
        }elseif ($sourceType == '销售预测单') {
            $all = find(array('table' => 'machining_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from machining_order_mx where coid = '" . self::$coid . "' and (sourceId in (select id from product_demands where sourceId =  '$sourceId') or sourceId in (select id from sale_forecast_detail where orderId =  '$sourceId'))) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('machiningOrder','see'),
                    'orderType' => '生产计划单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=machiningOrder&fun=detail',
                ];
                $temp['children'] = $this->machiningInOrder($array['id'], '生产计划单');
                $temp['children'] = array_merge($temp['children'], $this->machiningSendOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->machiningCard($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->semiOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->outsourceSendOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->pickOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->refundPickOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->outsourceOrder($array['id'], '生产计划单'));
                $temp['children'] = array_merge($temp['children'], $this->ingredientsOrder($array['id']));
                $result[] = $temp;
            }
        }elseif($sourceType =='生产计划单'){
            $info = query('machining_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('machiningOrder','see'),
                'orderType' => '生产计划单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=machiningOrder&fun=detail',
            ];
            $temp['children'] = $this->machiningInOrder($info['id'], '生产计划单');
            $temp['children'] = array_merge($temp['children'], $this->machiningSendOrder($info['id'], '生产计划单'));
            $temp['children'] = array_merge($temp['children'], $this->machiningCard($info['id'], '生产计划单'));
            $temp['children'] = array_merge($temp['children'], $this->semiOrder($info['id'], '生产计划单'));
            $temp['children'] = array_merge($temp['children'], $this->outsourceSendOrder($info['id'], '生产计划单'));
            $temp['children'] = array_merge($temp['children'], $this->pickOrder($info['id'], '生产计划单'));
            $temp['children'] = array_merge($temp['children'], $this->refundPickOrder($info['id'], '生产计划单'));
            $temp['children'] = array_merge($temp['children'], $this->outsourceOrder($info['id'], '生产计划单'));
            $temp['children'] = array_merge($temp['children'], $this->ingredientsOrder($info['id']));
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 半成品加工单
     * 从生产计划生成（传入生产计划ID）
     *
     */
    public function semiOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '生产计划单') {
            $all = find(array('table' => 'machining_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from machining_order_mx where coid = '" . self::$coid . "' and sourceId in (select id from semi_demands where sourceId =  '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('semiMachiningOrder','see'),
                    'orderType' => '半成品加工单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=semiMachiningOrder&fun=detail',
                ];
                $temp['children'] = $this->machiningInOrder($array['id'], '半成品加工单');
                $temp['children'] = array_merge($temp['children'], $this->machiningSendOrder($array['id'], '半成品加工单'));
                $temp['children'] = array_merge($temp['children'], $this->machiningCard($array['id'], '半成品加工单'));
                $temp['children'] = array_merge($temp['children'], $this->outsourceSendOrder($array['id'], '半成品加工单'));
                $temp['children'] = array_merge($temp['children'], $this->pickOrder($array['id'], '半成品加工单'));
                $temp['children'] = array_merge($temp['children'], $this->refundPickOrder($array['id'], '半成品加工单'));
                $temp['children'] = array_merge($temp['children'], $this->ingredientsOrder($array['id'],'半成品加工单'));
                $result[] = $temp;
            }
        }elseif($sourceType =='半成品加工单'){
            $info = query('machining_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('semiMachiningOrder','see'),
                'orderType' => '半成品加工单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=semiMachiningOrder&fun=detail',
            ];
            $temp['children'] = $this->machiningInOrder($info['id'], '半成品加工单');
            $temp['children'] = array_merge($temp['children'], $this->machiningSendOrder($info['id'], '半成品加工单'));
            $temp['children'] = array_merge($temp['children'], $this->machiningCard($info['id'], '半成品加工单'));
            $temp['children'] = array_merge($temp['children'], $this->outsourceSendOrder($info['id'], '半成品加工单'));
            $temp['children'] = array_merge($temp['children'], $this->pickOrder($info['id'], '半成品加工单'));
            $temp['children'] = array_merge($temp['children'], $this->refundPickOrder($info['id'], '半成品加工单'));
            $temp['children'] = array_merge($temp['children'], $this->ingredientsOrder($info['id'],'半成品加工单'));
            $result[] = $temp;
        }
        return $result;
    }
    /**
     * 生产入库单
     */
    public function machiningInOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '生产计划单' || $sourceType == '半成品加工单') {
            $all = find(array('table' => 'machining_in_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from machining_in_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from machining_order_mx where orderId =  '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('machiningInOrder','see'),
                    'orderType' => '生产入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=machiningInOrder&fun=detail',
                ];
                $temp['children'] = $this->inStock($array['id'], '生产入库单');
                $temp['children'] = array_merge($temp['children'], $this->qualityOrder($array['id'], '生产入库单'));
                $result[] = $temp;
            }
        }elseif($sourceType =='生产入库单'){
            $info = query('machining_in_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('machiningInOrder','see'),
                'orderType' => '生产入库单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=machiningInOrder&fun=detail',
            ];
            $temp['children'] = $this->inStock($info['id'], '生产入库单');
            $temp['children'] = array_merge($temp['children'], $this->qualityOrder($info['id'], '生产入库单'));
            $result[] = $temp;
        }
        return $result;
    }


    /**
     * 销售发货单
     * 从销售单中生成（传入销售单ID）
     * @param $sourceId
     * @param string $sourceType
     * @return array
     */
    public function saleSendOrder($sourceId,$sourceType='销售订单'): array
    {
        $result = [];
        if ($sourceType =='销售订单'){
            $all = find(array('table' => 'sale_send_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from sale_send_order_detail where coid = '" . self::$coid . "' and saleOrderMxId in (select id from sale_order_detail where orderId =  '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('saleSendOrder','see'),
                    'orderType' => '销售发货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=saleSendOrder&fun=detail',
                ];
                $temp['children'] = $this->saleRefundOrder($array['id'], '销售发货单');
                $temp['children'] = array_merge($temp['children'], $this->qualityOrder($array['id'], '销售发货单'));
                $temp['children'] = array_merge($temp['children'], $this->outStock($array['id'], '销售发货单'));
                $result[] = $temp;
            }
        }elseif($sourceType =='销售发货单'){
            $info = query('sale_send_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('saleSendOrder','see'),
                'orderType' => '销售发货单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=saleSendOrder&fun=detail',
            ];
            $temp['children'] = $this->saleRefundOrder($info['id'], '销售发货单');
            $temp['children'] = array_merge($temp['children'], $this->qualityOrder($info['id'], '销售发货单'));
            $temp['children'] = array_merge($temp['children'], $this->outStock($info['id'], '销售发货单'));
            $result[] = $temp;
        }

        return $result;
    }

    /**
     * 销售退货单
     * 从销售发货单生成（传入销售发货单ID）
     * 从销售订单生成（传入销售订单ID）
     * @param $sourceId
     * @param $sourceType
     * @return array
     */
    public function saleRefundOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '销售订单') {
            $all = find(array('table' => 'sale_refund_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from sale_refund_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from sale_order_detail where orderId =  '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('saleRefundOrder','see'),
                    'orderType' => '销售退货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => '销售订单',
                    'url' => root . 'control/adIndex.html?cla=saleRefundOrder&fun=detail',
                    'children' => $this->inStock($array['id'], '销售退货单')
                ];
                $result[] = $temp;
            }
        } elseif ($sourceType == '销售发货单') {
            $all = find(array('table' => 'sale_refund_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from sale_refund_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from sale_send_order_detail where orderId =  '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('saleRefundOrder','see'),
                    'orderType' => '销售退货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => '销售发货单',
                    'url' => root . 'control/adIndex.html?cla=saleRefundOrder&fun=detail',
                    'children' => $this->inStock($array['id'], '销售退货单')
                ];
                $result[] = $temp;
            }
        }elseif($sourceType =='销售退货单'){
            $info = query('sale_refund_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('saleRefundOrder','see'),
                'orderType' => '销售退货单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=saleRefundOrder&fun=detail',
                'children' => $this->inStock($info['id'], '销售退货单')
            ];
            $result[] = $temp;
        }
        return $result;
    }


    /**
     * 从采购需求生成（传入销售订单ID）
     * @param $sourceId
     * @return array
     */
    public function purchaseApply($sourceId): array
    {
        $result = [];
        $all = find(array('table' => 'purchase_apply', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from purchase_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_demands where sourceId =  '$sourceId')) order by recordDate,orderNo"));
        foreach ($all['array'] as $array) {
            $temp = [
                'power'=>$this->power('purchaseApply','see'),
                'orderType' => '采购订单',
                'orderNo' => $array['orderNo'],
                'id' => $array['id'],
                'img' => self::icon,
                'sourceId' => $sourceId,
                'sourceType' => '销售订单',
                'url' => root . 'control/adIndex.html?cla=purchaseApply&fun=detail',
            ];
            $temp['children'] = $this->purchaseGetOrder($array['id']);
            $temp['children'] = array_merge($temp['children'], $this->purchaseRefundOrder($array['id'], '采购订单'));
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 采购订单
     * 从采购需求生成(传入采购申请ID)
     * 从外购产品需求生成（传入销售单ID）
     * @param $sourceId
     * @param $sourceType
     * @return array
     */
    public function purchaseOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '采购申请单') {
            $all = find(array('table' => 'purchase_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from purchase_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_apply_detail where orderId =  '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('purchaseOrder','see'),
                    'orderType' => '采购订单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=purchaseOrder&fun=detail',
                ];
                $temp['children'] = $this->purchaseGetOrder($array['id']);
                $temp['children'] = array_merge($temp['children'], $this->purchaseRefundOrder($array['id'], '采购订单'));
                $result[] = $temp;
            }
        } elseif ($sourceType == '销售订单') {
            $all = find(array('table' => 'purchase_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from purchase_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_demands where sourceId =  '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('purchaseOrder','see'),
                    'orderType' => '采购订单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=purchaseOrder&fun=detail',
                ];
                $temp['children'] = $this->purchaseGetOrder($array['id']);
                $temp['children'] = array_merge($temp['children'], $this->purchaseRefundOrder($array['id'], '采购订单'));
                $result[] = $temp;
            }
        } elseif ($sourceType == '采购订单') {
            $info = query('purchase_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('purchaseOrder','see'),
                'orderType' => '采购订单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=purchaseOrder&fun=detail',
            ];
            $temp['children'] = $this->purchaseGetOrder($info['id']);
            $temp['children'] = array_merge($temp['children'], $this->purchaseRefundOrder($info['id'], '采购订单'));
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 采购收货单
     * 从采购订单生成（传入采购订单ID）
     * @param $sourceId
     * @param string $sourceType
     * @return array
     */
    public function purchaseGetOrder($sourceId,$sourceType='采购订单'): array
    {
        $result = [];
        if ($sourceType == '采购订单'){
            $all = find(array('table' => 'purchase_get_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from purchase_get_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_order_detail where orderId = '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $temp = [
                    'power'=>$this->power('purchaseGetOrder','see'),
                    'orderType' => '采购收货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => '采购订单',
                    'url' => root . 'control/adIndex.html?cla=purchaseGetOrder&fun=detail',
                ];
                $temp['children'] = $this->inStock($array['id'], '采购收货单');
                $temp['children'] = array_merge($temp['children'], $this->purchaseRefundOrder($array['id'], '采购收货单'));
                $temp['children'] = array_merge($temp['children'], $this->qualityOrder($array['id'], '采购收货单'));
                $result[] = $temp;
            }
        }elseif ($sourceType == '采购收货单') {
            $info = query('purchase_get_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('purchaseGetOrder','see'),
                'orderType' => '采购收货单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=purchaseOrder&fun=detail',
            ];
            $temp['children'] = $this->inStock($info['id'], '采购收货单');
            $temp['children'] = array_merge($temp['children'], $this->purchaseRefundOrder($info['id'], '采购收货单'));
            $temp['children'] = array_merge($temp['children'], $this->qualityOrder($info['id'], '采购收货单'));
            $result[] = $temp;
        }

        return $result;
    }

    /**
     * 采购退货单
     * 从采购订单生成（传入采购订单ID）
     * 从采购退货单生成（传入采购退货单ID）
     */
    public function purchaseRefundOrder($sourceId, $sourceType): array
    {
        $result = [];
        if ($sourceType == '采购订单') {
            $all = find(array('table' => 'purchase_refund_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from purchase_refund_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_order_detail where orderId = '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('purchaseRefundOrder','see'),
                    'orderType' => '采购退货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => '采购订单',
                    'url' => root . 'control/adIndex.html?cla=purchaseRefundOrder&fun=detail',
                    'children' => $this->outStock($array['id'], '采购退货单')
                ];
            }
        } elseif ($sourceType == '采购收货单') {
            $all = find(array('table' => 'purchase_refund_order', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from purchase_refund_order_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_get_order_detail where orderId = '$sourceId')) order by recordDate,orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('purchaseRefundOrder','see'),
                    'orderType' => '采购退货单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => '采购收货单',
                    'url' => root . 'control/adIndex.html?cla=purchaseRefundOrder&fun=detail',
                    'children' => $this->outStock($array['id'], '采购退货单')
                ];
            }
        }elseif ($sourceType == '采购退货单') {
            $info = query('purchase_refund_order', "id = '$sourceId'");
            $temp = [
                'power'=>$this->power('purchaseRefundOrder','see'),
                'orderType' => '采购退货单',
                'orderNo' => $info['orderNo'],
                'id' => $info['id'],
                'img' => self::icon,
                'sourceId' => '',
                'sourceType' => '',
                'url' => root . 'control/adIndex.html?cla=purchaseRefundOrder&fun=detail',
            ];
            $temp['children'] = $this->outStock($info['id'], '采购退货单');
            $result[] = $temp;
        }
        return $result;
    }

    /**
     * 入库单
     * 从采购收货单生成（传入采购收货单ID）
     *
     */
    public function inStock($sourceId, $sourceType): array
    {
        $where = self::getWarehouseSql('warehouseId');
        $result = [];
        if ($sourceType == '采购收货单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_get_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('inStock','see'),
                    'orderType' => '入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=inStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '生产入库单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from machining_in_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('inStock','see'),
                    'orderType' => '入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=inStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '销售退货单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from sale_refund_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('inStock','see'),
                    'orderType' => '入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=inStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '委外退料单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_pick_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('inStock','see'),
                    'orderType' => '入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=inStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '退料单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from pick_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('inStock','see'),
                    'orderType' => '入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=inStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '委外收货单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_in_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('inStock','see'),
                    'orderType' => '入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=inStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '报废申请单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId  = '$sourceId') $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('inStock','see'),
                    'orderType' => '入库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=inStock&fun=detail',
                    'children' => []
                ];
            }
        }
        return $result;
    }

    /**
     * 出库单
     */
    public function outStock($sourceId, $sourceType): array
    {
        $where = self::getWarehouseSql('warehouseId');
        $result = [];
        if ($sourceType == '销售发货单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from sale_send_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('outStock','see'),
                    'orderType' => '出库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '采购退货单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from purchase_refund_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('outStock','see'),
                    'orderType' => '出库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '委外发料单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_pick_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('outStock','see'),
                    'orderType' => '出库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outStock&fun=detail',
                    'children' => []
                ];
            }
        } elseif ($sourceType == '委外退货单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from outsource_refund_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('outStock','see'),
                    'orderType' => '出库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outStock&fun=detail',
                    'children' => []
                ];
            }
        }elseif ($sourceType == '领料单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from pick_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('outStock','see'),
                    'orderType' => '出库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outStock&fun=detail',
                    'children' => []
                ];
            }
        }elseif ($sourceType == '配料单') {
            $all = find(array('table' => 'inventory', 'where' => "coid = '" . self::$coid . "' and id in (select distinct orderId from inventory_detail where coid = '" . self::$coid . "' and sourceId in (select id from ingredients_order_detail where orderId = '$sourceId')) $where order by orderNo"));
            foreach ($all['array'] as $array) {
                $result[] = [
                    'power'=>$this->power('outStock','see'),
                    'orderType' => '出库单',
                    'orderNo' => $array['orderNo'],
                    'id' => $array['id'],
                    'img' => self::icon,
                    'sourceId' => $sourceId,
                    'sourceType' => $sourceType,
                    'url' => root . 'control/adIndex.html?cla=outStock&fun=detail',
                    'children' => []
                ];
            }
        }
        return $result;
    }
}

